package com.example.li.service;

import com.example.li.mapper.UserMapper;
import com.example.li.model.User;
import com.example.li.util.ResultDTO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
public class UserService {
    @Autowired
    UserMapper userMapper;

    //查询用户（登录）
    public ResultDTO login(User user) {
        List<User> user1 = userMapper.selectByLawUserNumber(user);
        if (user1 != null && user1.size() > 0) {
            if (user1.get(0).getLawUserPwd().equals(user.getLawUserPwd())) {
                return new ResultDTO().success(user1);
            } else {
                return new ResultDTO().failure("密码错误！");
            }
        } else {
            return new ResultDTO().failure("信息错误！");
        }
    }

    //用户添加（注册）
    public ResultDTO insert(User user) {
        List<User> users = userMapper.selectByLawUserNumber(user);
        if (users.size() == 0) {
            user.setLawUserAddtime(new Date());
            int i = userMapper.insertSelective(user);
            return new ResultDTO().success();
        } else {
            return new ResultDTO().failure("该用户已经存在!");
        }
    }

    //修改
    public ResultDTO update(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        return new ResultDTO().success();
    }

    //用户列表
    public List getUserList(User user) {
        //PageHelper.startPage(1, 1);
        List<User> users = userMapper.selectByLawUserNumber(user);
        return users;
    }

    //删除用户
    public ResultDTO deleteUser(User user) {
        int i = userMapper.deleteByPrimaryKey(user.getId());
        return new ResultDTO().success();
    }

    //通过id找一个用户
    public ResultDTO showUser(User user) {
        User user1 = userMapper.selectByPrimaryKey(user.getId());
        return new ResultDTO().success(user1);
    }

}